package testJdk.commonUse.string.stringBuilder;

import org.junit.Test;

import java.util.Scanner;

/**
 * @author keyer
 * @Description : 描述
 * Created by cyd on 2021-04-25 17:33:24
 */
public class TestStringBuilder {


    /**
     * 构造
     */
    @Test
    public void test01() {
        StringBuilder stringBuilder = new StringBuilder(100);
        int capacity = stringBuilder.capacity();
        System.out.println(capacity);
    }

    /**
     * 拼接
     */
    @Test
    public void test02() {
        StringBuilder plate = new StringBuilder("鄂A");
        plate.append("10000");
        System.out.println(plate);
    }

    /**
     * 替换
     * 使用双指针来定位，两个指针之间的部分被替换
     */
    @Test
    public void test03() {
        StringBuilder s = new StringBuilder("12345678");
        // 使用双指针来定位，两个指针之间的部分被替换
        s.replace(1, 2, "sss");
        System.out.println(s.toString());
    }

    /**
     * 删除
     * 使用双指针来定位，两个指针之间的部分被删除
     */
    @Test
    public void test04() {
        StringBuilder s = new StringBuilder("12345678");
        // 使用双指针来定位，两个指针之间的部分被替换
        s.delete(1, 2);
        System.out.println(s.toString());
    }

    /**
     * 查找
     * indexOf 查找某个字符在字符串第一次出现的位置
     * lastIndexOf 从右往左查找某个字符最后(这个最后是左面>>>右边)出现在字符串的位置
     */
    @Test
    public void test05() {
        StringBuilder s = new StringBuilder("abccccd");
        int index = s.indexOf("c");
        System.out.println(index);
        int lastIndex = s.lastIndexOf("c");
        System.out.println(lastIndex);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入回文串：");

        String s = scanner.next();
        System.out.println(s + "是回文串吗？" + isPalindrome(s));

        scanner.close();
    }

    private static boolean isPalindrome(String s) {
        String s1 = filter(s);
        String s2 = reverse(s1);
        return s1.equals(s2);
    }

    private static String reverse(String s) {
        StringBuilder stringBuilder = new StringBuilder(s);
        stringBuilder.reverse();
        return stringBuilder.toString();
    }

    private static String filter(String s) {
        StringBuilder stringBuilder = new StringBuilder(s);
        for (int i = 0; i < s.length(); i++) {
            if (Character.isLetterOrDigit(s.charAt(i))) {
                stringBuilder.append(s.charAt(i));
            }
        }
        System.out.println(stringBuilder);
        return stringBuilder.toString();
    }

}
